package com.mylib.widget;

import android.graphics.Rect;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.view.View;


public class StaggeredSpaceItemDecoration extends RecyclerView.ItemDecoration {
    private static final String TAG = StaggeredSpaceItemDecoration.class.getName();
    private int space;
    private int columnCount;

    public StaggeredSpaceItemDecoration(int space, int columnCount) {
        this.space = space;
        this.columnCount = columnCount;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        //super.getItemOffsets(outRect, view, parent, state);
//        if (parent.getChildViewHolder(view) instanceof HomeAdapter.FormalThreeVH) {}
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();

        if (layoutManager instanceof StaggeredGridLayoutManager) {

            //瀑布流专属分割线
            StaggeredGridLayoutManager.LayoutParams params =
                    (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
            /**
             * 根据params.getSpanIndex()来判断左右边确定分割线
             * 第一列设置左边距为space，右边距为space/2  （第二列反之）
             */
            outRect.top = space;
            if (params.getSpanIndex() % 2 == 0) {
                outRect.left = space;
                outRect.right = space / 2;
            } else {
                outRect.left = space / 2;
                outRect.right = space;
            }
            /*int position = parent.getChildAdapterPosition(view);
            if (position != 0) {
                //outRect.top = space;
                if (params.getSpanIndex() % 2 == 0) {
                    outRect.left = space;
                    outRect.right = space / 2;
                } else {
                    outRect.left = space / 2;
                    outRect.right = space;
                }
            }*/
        }
    }
}
